import 'package:flutter/material.dart';
import 'package:gridviewdemo/colors/colors.dart';
import 'package:gridviewdemo/models/models.dart';
import 'package:gridviewdemo/simple/simple_puzzle_Tile.dart';

/// {@template simple_puzzle_board}
/// Display the board of the puzzle in a [size]x[size] layout
/// filled with [tiles]. Each tile is spaced with [spacing].
/// {@endtemplate}
@visibleForTesting
class SimplePuzzleBoard extends StatelessWidget {
  /// {@macro simple_puzzle_board}
  const SimplePuzzleBoard({
    Key? key,
    required this.size,
    required this.tiles,
    this.spacing = 0,
  }) : super(key: key);

  /// The size of the board.
  final int size;

  /// The tiles to be displayed on the board.
  final List<Widget> tiles;

  /// The spacing between each tile from [tiles].
  final double spacing;

  @override
  Widget build(BuildContext context) {
    return GridView.count(
      padding: EdgeInsets.zero,
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      crossAxisCount: size,
      mainAxisSpacing: spacing,
      crossAxisSpacing: spacing,
      children: tiles,
    );
  }
}

class PuzzleTile extends StatelessWidget {
  const PuzzleTile({
    Key? key,
    required this.tile,
  }) : super(key: key);

  /// The tile to be displayed.
  final Tile tile;

  @override
  Widget build(BuildContext context) {
//    final state = context.select((PuzzleBloc bloc) => bloc.state);

    return tile.isWhitespace
        ? whitespaceTileBuilder()
        : tileBuilder(tile);
  }

  Widget whitespaceTileBuilder() {
    return const SizedBox();
  }

  Widget tileBuilder(Tile tile) {
    return SimplePuzzleTile(
      key: Key('simple_puzzle_tile_${tile.value}_small'),
      tile: tile,
      tileFontSize: 60,
    );
  }
}
